part of common_lib_plugin;

extension IntExtension on int {
  String formatVideoDuration() {
    int oneMinute = 60;
    int oneHour = oneMinute * 60;
    if (this < oneMinute) {
      return '00:${'$this'.fillingPrefixZero(2)}';
    } else if (this < oneHour) {
      return '${'${(this ~/ oneMinute)}'.fillingPrefixZero(2)}:${'${(this % oneMinute)}'.fillingPrefixZero(2)}';
    } else {
      return '${(this ~/ oneHour)}:${'${(this % oneHour ~/ oneMinute)}'.fillingPrefixZero(2)}:${'${(this % oneHour % oneMinute)}'.fillingPrefixZero(2)}';
    }
  }

  String formatPostInterval() {
    int oneMinute = 1000 * 60;
    int oneHour = oneMinute * 60;
    int oneDay = oneHour * 24;
    int oneMonth = oneDay * 30;
    int oneYear = oneDay * 365;
    if (this < oneHour) {
      return 'new';
    } else if (this < oneDay) {
      return '${(this / oneHour).round()}h';
    } else if (this < oneMonth) {
      return '${(this / oneDay).round()}d';
    } else if (this < oneYear) {
      return '${(this / oneMonth).round()}m';
    } else {
      return '${(this / oneYear).round()}y';
    }
  }

  String formatTime(String formatStr, {bool isUtc = false}) {
    var time = this;
    if (this < 9999999999) {
      time = this * 1000;
    }
    List<String> monthList = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    List<String> eMonthList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    DateTime date = DateTime.fromMillisecondsSinceEpoch(time, isUtc: isUtc);
    var year = date.year.toString();
    var month = date.month.toString();
    var eMonth = monthList[date.month - 1];
    var wholeEMonth = eMonthList[date.month - 1];
    var day = date.day.toString();
    var hour = date.hour.toString();
    var minute = date.minute.toString();
    var second = date.second.toString();
    formatStr = formatStr
        .replaceAll('YYYY', year.fillingPrefixZero(4))
        .replaceAll('MM', month.fillingPrefixZero(2))
        .replaceAll('ee', eMonth)
        .replaceAll('EE', wholeEMonth)
        .replaceAll('dd', day.fillingPrefixZero(2))
        .replaceAll('HH', hour.fillingPrefixZero(2))
        .replaceAll('mm', minute.fillingPrefixZero(2))
        .replaceAll('ss', second.fillingPrefixZero(2));
    return formatStr;
  }
}
